<?php

namespace App\Admin\Controllers;

use App\Models\AppVersion;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class VersionController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'App\Models\AppVersion';

    private static $osType = [
        1  => 'Android',
        2  => 'IOS',
    ];
    private static $updateType = [
        1 => '提示更新',
        2 => '不提示更新',
        3 => '强制更新'
    ];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new AppVersion);

        $grid->model()->orderBy('id', 'desc');
        $grid->model()->take(20);


        $grid->column('id', __('Id'));
        $grid->column('os_type', __('操作系统'))->display(function ($osType){
            return self::$osType[$osType];
        });
        $grid->column('version', __('版本号'));
        $grid->column('review_version', __('审核版本号'));
        $grid->column('update_type', __('更新类型'))->display(function ($updateType){
            return self::$updateType[$updateType];
        });
        $grid->column('update_url', __('更新url'))->downloadable();
        $grid->column('update_title', __('更新标题'));
        $grid->column('update_content', __('更新内容'));
        $grid->column('update_icon', __('更新icon'));
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('修改时间'));

        //禁用导出数据按钮
        $grid->disableExport();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(AppVersion::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('os_type', __('操作系统'))->using(self::$osType);
        $show->field('version', __('版本号'));
        $show->field('review_version', __('审核版本号'));
        $show->field('update_type', __('更新类型'))->using(self::$updateType);
        $show->field('update_url', __('更新url'))->link();
        $show->field('update_title', __('更新标题'));
        $show->field('update_content', __('更新内容'));
        $show->field('update_icon', __('更新icon'));
        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('修改时间'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new AppVersion);

        $form->text('version', __('版本号'));

        $form->radio('os_type', __('操作系统'))->options(self::$osType)->when(1, function (Form $form){
            // 异步上传文件
            $form->file('files',  __('上传安装包'))->options([
                'showPreview' => false,
                'allowedFileExtensions'=>['apk'],
                'showUpload'=>true,
                'uploadAsync' =>true,
                'uploadUrl' => '/admin/file_upload',
                'uploadExtraData' => [
                    '_token'    => csrf_token(),
                    '_method'   => 'POST',
                ],
            ])->default('update_url');
            $form->hidden('update_url');

        })->when(2, function (Form $form) {
            $form->text('update_url', __('更新url'));
            $form->text('review_version', __('审核版本号'));
        });
        $form->radio('update_type', __('更新类型'))->options(self::$updateType);
        $form->text('update_title', __('更新标题'));
        $form->text('update_content', __('更新内容'));
        $form->text('update_icon', __('更新icon'));


        /*给隐藏域赋值*/
        $js = <<<EOT
        $('input.files').on ('fileuploaded', function (event, data, id, index) { 
            var res = data.response;
            console.log(res);
            $('input.update_url').val(res.data); 
        });
EOT;
        \Encore\Admin\Admin::script ($js);

//        // 在表单提交前调用
        $form->submitted(function (Form $form) {

        });

        //保存前回调
        $form->saving(function (Form $form) {

        });

        //保存后回调
        $form->saved(function (Form $form) {
            //删除版本缓存
            AppVersion::updateVersionCache($form->model()->os_type);
        });


        return $form;
    }
}
